/**
 * 
 */
package com.jolestar.yayaweibotong.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.jolestar.yayaweibotong.dao.AccountDomain;
import com.jolestar.yayaweibotong.dao.UserDomain;
import com.jolestar.yayaweibotong.service.UserService;

/**
 * @author jolestar@gmail.com
 *
 */
public class AccountAction extends AbstractAction {

	/**
	 * 
	 */
	public AccountAction() {
		super("account");
	}

	/* (non-Javadoc)
	 * @see com.jolestar.yayaweibotong.controller.AbstractAction#doAction(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected String doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		UserDomain user = (UserDomain) request.getSession().getAttribute("user");
		if("delete".equals(method)){
			String key = request.getParameter("key");
			return this.deleteAccount(request,user,key);
		}
		return "error:400";
	}
	
	private String deleteAccount(HttpServletRequest request,UserDomain user,String key){
		if(StringUtils.isBlank(key)){
			return "error:400";
		}
		AccountDomain account = UserService.ServiceHolder.getInstance().getAccount(key);
		if(account != null){
			UserService.ServiceHolder.getInstance().deleteAccount(key);
		}
		user.reloadAccount();
		request.getSession().setAttribute("user", user);
		return "redirect:/dashboard";
	}

	@Override
	public String getPath() {
		return "/"+this.getName();
	}

}
